﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace hse_2013_sapper
{
    public partial class MainForm : Form
    {
        Sapper.Sapper field;
        Button[,] b;
        Label lb1;
        Label lb2;
        public int m = 10, n = 10, bomb = 10;
        int time = 0;
        int[,] mass;
        int t = 0;

        public MainForm()
        {
            InitializeComponent();
            field = new Sapper.Sapper();
            mass = field.SozdIgri(m, n, bomb);
            InitB();
            SettingsForm.OnObjectChanged += new SettingsForm.ObjectChangedCallback(Redraw);
        }

        void Redraw(int l, int k, int o)
        {
            Rem();
            m = l;
            n = k;
            bomb = o;
        }

        public void InitB()
        {
            b = new Button[m, n];
            this.Size = new Size((m * 25) + 36, (n * 25) + 101);

            int i, j;

            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    b[i, j] = new Button();
                    t++;
                    b[i, j].Location = new System.Drawing.Point((i * 25) + 10, (j * 25) + 55);
                    b[i, j].Name = "button" + i.ToString() + j.ToString();
                    b[i, j].Size = new System.Drawing.Size(25, 25);
                    b[i, j].TabStop = false;
                    b[i, j].Tag = i * 1000 + j;
                    b[i, j].Text = "";
                    b[i, j].UseVisualStyleBackColor = true;
                    b[i, j].Visible = true;
                    b[i, j].BackColor = System.Drawing.Color.FromArgb(150, 150, 255);
                    b[i, j].MouseDown += new MouseEventHandler(B_Click);
                    this.Controls.Add(b[i, j]);
                }
            }

            lb1 = new Label();
            lb1.Location = new Point(10, 30);
            lb1.Text = "Время: 0";
            this.Controls.Add(lb1);

            lb2 = new Label();
            lb2.Location = new Point(this.Width - 77, 30);
            lb2.Text = "Бомб: " + bomb.ToString();
            this.Controls.Add(lb2);
        }

        private void B_Click(object sender, MouseEventArgs e)
        {
            timer1.Start();
            Font f = new Font("Microsoft Tahoma", 12F);
            SolidBrush br = new SolidBrush(Color.Red);

            if (e.Button == MouseButtons.Left)
            {
                if (((Button)sender).BackColor == Color.Red)
                {

                }
                else
                {
                    int j = 0;
                    int i = 0;
                    for (int l = 0; l < m; l++)
                    {
                        for (int k = 0; k < n; k++)
                        {
                            if (b[l, k].Tag == ((Button)sender).Tag)
                            {
                                i = l;
                                j = k;
                                break;
                            }
                        }
                    }
                    OpenButton(i, j);
                    Proverka(i, j);
                }
            }
            if (e.Button == MouseButtons.Right)
            {
                if (((Button)sender).BackColor == Color.Red)
                {
                    ((Button)sender).BackColor = System.Drawing.Color.FromArgb(150, 150, 255);
                }
                else
                {
                    ((Button)sender).BackColor = Color.Red;
                }
            }
        }

        private void Mark()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (field.Market[i, j] == true)
                    {
                        b[i, j].ForeColor = Color.Red;
                        b[i, j].Enabled = false;
                        b[i, j].BackColor = Color.FromArgb(220, 220, 255);
                        b[i, j].Text = mass[i, j].ToString();
                    }
                }
            }
        }

        private void Obn()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    b[i, j].Text = "";
                    b[i, j].UseVisualStyleBackColor = true;
                    b[i, j].Visible = true;
                    b[i, j].BackColor = System.Drawing.Color.FromArgb(150, 150, 255);
                    b[i, j].Enabled = true;
                }
            }
        }

        private void OpenButton(int i, int j)
        {
            b[i, j].BackColor = Color.FromArgb(220, 220, 255);
            b[i, j].Enabled = false;
            b[i, j].ForeColor = Color.Red;
            field.Market[i, j] = true;
            field.schetchik++;
        }

        private void Proverka(int i, int j)
        {
            if (mass[i, j] == -1)
            {
                timer1.Stop();
                b[i, j].Text = "B";
                MessageBox.Show("Вы проиграли!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Obn();
                lb1.Text = "Время: 0";
                field = new Sapper.Sapper();
                mass = field.SozdIgri(m, n, bomb);
                time = 0;
            }
            else
            {
                b[i, j].Text = mass[i, j].ToString();
                if (mass[i, j] == 0)
                {
                    field.bfs(i, j);
                    Mark();
                }
            }
            if (field.schetchik == m * n - bomb)
            {
                timer1.Stop();
                DialogResult dr = MessageBox.Show("Вы выиграли!\nВаше время - " + time.ToString() + "сек.\nХотите сыграть еще раз?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                {
                    if (dr == DialogResult.Yes)
                    {
                        lb1.Text = "Время: 0";
                        time = 0;
                        Obn();
                        field = new Sapper.Sapper();
                        mass = field.SozdIgri(m, n, bomb);
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
        }

        private void новаяИграToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            lb1.Text = "Время: 0";
            time = 0;

            Obn();
            field = new Sapper.Sapper();
            mass = field.SozdIgri(m, n, bomb);

        }

        private void параметрыToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingsForm form2 = new SettingsForm();
            form2.ShowDialog();
            InitB();
            Obn();
            field = new Sapper.Sapper();
            mass = field.SozdIgri(m, n, bomb);
        }

        private void Rem()
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    this.Controls.Remove(b[i, j]);
                }
            }
            this.Controls.Remove(lb1);
            this.Controls.Remove(lb2);
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (time < 999)
            {
                time++;
                lb1.Text = "Время: " + time.ToString();
            }
        }

        private void оПрограммеToolStripMenuItem_Click(object sender, EventArgs e)
        {
            InfoForm form3 = new InfoForm();
            form3.ShowDialog();
        }

        private void выходToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
